package com.data.modules.es;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.data.common.es.ESTemplate;
import com.data.modules.demo.biz.mapper.DemoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class InitEsIndexRunner /*implements ApplicationRunner*/ {

    @Autowired
    private DemoMapper demoMapper;

    /**
     * 启动项目同步es数据
     */
    @Autowired
    private ESTemplate restTemplate;

    public void run(ApplicationArguments args) throws Exception {
        String index = "data_boot_acs";
        log.info("es {} index init start!",index);


        if (!restTemplate.indexExists(index)){
            createIndex(index);
            initData(index);
            log.info("es {} index init success!",index);
        }

        //查询es
        String keyWord = "pressure dependence of the colloidal phenomena of nanoparticles in hydrothermal";
        JSONObject search = search(index, keyWord);
        log.info("查询结果：{}",search.toJSONString());
    }

    private JSONObject search(String index, String keyWord) {
        JSONObject abstractTextNoHtmllabel = restTemplate.buildQueryString("wildcard","AbstractTextNoHtmllabel", "*"+keyWord+"*");
        JSONObject title = restTemplate.buildQueryString("wildcard","Title", "*"+keyWord+"*");

        JSONArray jsonArray = new JSONArray();

        jsonArray.add(abstractTextNoHtmllabel);
        jsonArray.add(title);

        JSONObject query = restTemplate.buildBoolQuery(null, null, jsonArray);
        JSONObject jsonObject = restTemplate.buildQuery(null, query, 0, 10);

        JSONObject doc = restTemplate.search(index, "_doc", jsonObject);
        return doc;
    }

    private void initData(String index) {
        JSONArray jsonArray = demoMapper.selectAcs();
        restTemplate.saveBatch(index,"_doc",jsonArray);
//        for (Object o : jsonArray) {
//            JSONObject jsonObject = (JSONObject) o;
//            restTemplate.save(index,"_doc",jsonObject.getString("id"),jsonObject);
//        }
    }

    private boolean createIndex(String index) {
        log.info("es {} create  start!",index);
        JSONObject params = new JSONObject();

        JSONObject settings = new JSONObject();
        JSONObject mapping = new JSONObject();
        JSONObject properties = new JSONObject();

        params = JSONObject.parseObject("{\n" +
                "    \"settings\": {\n" +
                "        \"index\": {\n" +
                "            \"number_of_shards\": \"1\",\n" +
                "            \"number_of_replicas\": \"0\"\n" +
                "        }\n" +
                "    },\n" +
                "    \"mappings\": {\n" +
                "        \"_doc\": {\n" +
                "            \"properties\": {\n" +
                "                \"id\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"Citations\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"JourPageRange\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"AuthorName\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"JourYear\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"Subject_name\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"JourVolNum\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"AbstractText\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"Altmetric\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"Keywords_list\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"CiteTitle\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"UpdateTableName\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"DOIUrl\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"TopTitle\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"ids\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"FullTextUrl\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"crawl_time\": {\n" +
                "                    \"type\": \"date\",\n" +
                "                    \"format\": \"yyyy-MM-dd HH:mm:ss\"\n" +
                "                },\n" +
                "                \"CopyRight\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"table_name\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"DataBaseShortName\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"ArticleViews\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"UpdateStatus\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"Title\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"JourPubDate\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"ListUrlMain\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"AbstractTextNoHtmllabel\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"articlePermissionsLink\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"JourIssueNum\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"AbstractImgUrl\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"PDFUrl\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                },\n" +
                "                \"isadded\": {\n" +
                "                    \"type\": \"keyword\"\n" +
                "                }\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}");

        //
        return restTemplate.createIndex(index,params);
    }



}
